package com.springboot.demo.mapper;

import com.springboot.demo.entity.ComponentPosition;
import com.springboot.demo.entity.MapData;
import com.springboot.demo.entity.MapLinkedData;
import com.springboot.demo.entity.PanelComponent;
import com.springboot.demo.entity.validate.MapCoordinateGroup;
import com.springboot.demo.entity.validate.user.IdGroup;
import io.swagger.annotations.ApiModelProperty;
import org.apache.ibatis.annotations.*;

import javax.validation.constraints.NotNull;

/**
 * @author wubc683
 * @desc
 * @date 2021-11-22 17:15
 */
public interface MapMapper {
    @Select("select id,color,disperseType,longitude,latitude,scale from web_map where id = (select mapId from web_panel_map " +
            "where panelId = " +
            "#{pid})")
    MapData getPanelMapByPanelId(Long pid);

    @Insert("insert into web_map(disperseType,longitude,latitude,color,scale) values(#{disperseType}," +
            "#{longitude},#{latitude},#{color},#{scale})")
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    void saveMap(MapData mapData);

    @Select("select id,color,disperseType,longitude,latitude,scale from web_map where id = " +
            "(select mapId from web_user_monitor_map where userId = #{uid})")
    MapData getMonitorMapByUid(Long uid);

    @Insert("insert into web_user_monitor_map(userId,mapId) values(#{uid},#{mapId})")
    void saveUserMonitorMap(Long uid, Long mapId);

    @Select("select id,color,disperseType,longitude,latitude,scale from web_map where id = #{mapId}")
    MapData getMonitorMapByMapId(Long mapId);

    @Select("select userId from web_user_monitor_map where mapId = #{mapId}")
    Long getUserIdByMapId(Long mapId);

    @Update("<script> update web_map set <if test='disperseType !=null and disperseType !=\"\"'> " +
            "disperseType=#{disperseType},</if>" +
            "<if test='longitude !=null and longitude !=\"\"'> longitude=#{longitude},</if>" +
            "<if test='latitude !=null and latitude !=\"\"'> latitude=#{latitude},</if>" +
            "<if test='color !=null and color !=\"\"'> color=#{color},</if>" +
            "<if test='scale !=null and scale !=\"\"'> scale=#{scale},</if> id=#{id} where id = #{id}</script>")
    void updateMapData(MapData mapData);

    @Update("update web_map set longitude=#{longitude},latitude=#{latitude},scale=#{scale}")
    void updateMapPosition(MapData mapData);

    @Select("select leftPos,rightPos,topPos,downPos from web_map_link where mapId = #{mapId}")
    ComponentPosition getMapLinkDataByMapId(Long mapId);

    @Insert("insert into web_map_link(mapId,leftPos,rightPos,topPos,downPos) values(#{mapId},#{component.position" +
            ".leftPos},#{component.position.rightPos},#{component.position.topPos},#{component.position.downPos})")
    void saveMapLink(Long mapId, PanelComponent component);

    @Update("update web_map_link set leftPos = #{component.position.leftPos},rightPos=#{component.position.rightPos}," +
            "topPos=#{component.position.topPos}," + "downPos=#{component.position.downPos} " +
            "where mapId = #{mapId}")
    void updateMapLink(Long mapId, PanelComponent component);

    @Delete("delete from web_map where id = #{mapId}")
    void deleteMap(Long mapId);

    @Delete("delete from web_map_link where mapId = #{mapId}")
    void deleteMapLink(Long mapId);

}
